package cn.ffcs.tasks;

import cn.ffcs.utils.DeleteExpiredFile;
import cn.ffcs.utils.FTPUtil;
import cn.ffcs.utils.FtpProcessor;
import cn.ffcs.utils.MailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

@Slf4j
@Component
public class ScheduledService {

    @Value("${localFilePath}")
    private String localFilePath;

    @Value("${remoteFilePath}")
    private String remoteFilePath;

    @Value("${failUploadFilePath}")
    private String failUploadFilePath;

    @Value("${warming.mails}")
    private String warmingEmail;

    @Value("${warming.title}")
    private String warmingTitle;

    @Resource
    private FtpProcessor ftpProcessor;

    //@Resource
    //private FTPUtil ftpUtil;

    @Resource
    MailService mailService;

    @Scheduled(cron = "${deleteTime}")
    public void delete(){
        LocalDate now = LocalDate.now();
        File localPath = new File(localFilePath + "/");
        File[] localPathFiles = localPath.listFiles();
        for (File f : localPathFiles) {
            LocalDate day = LocalDate.parse(f.getName());
            if (f.isDirectory() && isLast3Day(day,now)) {
                log.info("=====>>>>>删除" + f.getName() + "文件:");
                //删除目录下的文件
                DeleteExpiredFile.deleteExpiredFileTask(localFilePath + "/" + f.getName());
                File path = new File(localFilePath + "/" + f.getName());
                path.delete();
            }
        }
    }

    @Scheduled(cron = "${reUploadTime}")
    public void resend(){
        File filePath = new File(failUploadFilePath);
        int length = filePath.listFiles().length;
        if(length > 0) {
            log.info("=====>>>>>定时重传失败文件");
            if(length % 50 > 40) {
                mailService.sendTextMailMessage(warmingEmail, warmingTitle,"上传失败文件数量达到"+length+"，请确认是否存在问题");
            }
        }
        for (File file : filePath.listFiles()) {
            try {
                InputStream inputStream = new FileInputStream(file);
                boolean success = ftpProcessor.uploadToFtp(remoteFilePath, file.getName(), inputStream);
                inputStream.close();
                if(success) {
                    log.info("重传FTP服务器成功: " + file.getName());
                    //重传成功，删除本地文件
                    file.delete();
                } else {
                    log.error("重传FTP服务器失败: " + file.getName());
                }
            } catch (Exception e) {
                log.error("上传FTP服务器失败: " + file.getName());
                e.printStackTrace();
            }
        }
    }

    public boolean isLast3Day(LocalDate day, LocalDate now){
        return day.isBefore(now.plusDays(-3));
    }
}